#include "m0wig.h"
#include "ui_m0wig.h"

#define N 64
M0Wig::M0Wig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::M0Wig)
{
    ui->setupUi(this);

  udpsocket = new QUdpSocket(this);
  QHostAddress myudpip("192.168.31.137");
//  udpsocket->bind(QHostAddress::LocalHost, 7755);
  udpsocket->bind(myudpip, 7755);
  qDebug() << "Socket successfully bound to" << myudpip.toString() << "port" <<7755;


  datagram.resize(udpsocket->pendingDatagramSize());

  tcpsocket=new QTcpSocket(this);
  connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    connect(ui->pushButtonselect,SIGNAL(clicked()),this,SLOT(equip_select()));
    connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readPendingDatagrams()));
    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(btnConnClickedSlot()));

}

M0Wig::~M0Wig()
{
    delete ui;
}

int M0Wig::equip_select()
{
    qDebug()<<"检索设备";



    //创建udp服务,初始化
    QHostAddress sender;
    quint16 senderPort;
    datagram.append("hello");
    qDebug()<<datagram.data();
    sender.setAddress("192.168.31.255");

 qDebug()<<sender.toString();


    udpsocket->writeDatagram(datagram.data(), datagram.size(),sender, 8880);

//    while (udpsocket->hasPendingDatagrams()) {
//        QByteArray datagram;
//        datagram.resize(udpsocket->pendingDatagramSize());
//        QHostAddress sender;
//        quint16 senderPort;
//        udpsocket->readDatagram(datagram.data(), datagram.size(),
//                                &tcpsocketip, &tcpsocketPort);


//        qDebug()<<"返回信息";
//        qDebug()<<datagram.data();
//        tcpsocket->connectToHost(tcpsocketip,tcpsocketPort);
//        ui->textBrowser_equip->append("连接成功");
//        ui->textBrowser_equip->append(tcpsocketip.toString());

//    }
    qDebug()<<"准备连接";
    //获得ip和端口号


////    QString ip ="192.168.50.163";
//    ui->textBrowser_equip->setText("192.168.50.163");
////    int port = 7755;
////    // 连接到服务器
////    tcpsocket->connectToHost(tcpsocketip,tcpsocketPort);
//    ui->textBrowser_equip->append("连接成功");
    return 0;
}

void M0Wig::readPendingDatagrams()
{ qDebug()<<"返回信息";

    while (udpsocket->hasPendingDatagrams()) {

        QByteArray datagram;
        datagram.resize(udpsocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        udpsocket->readDatagram(datagram.data(), datagram.size(),
                                &tcpsocketip, &tcpsocketPort);
        QString str = QString::fromUtf8(datagram);
        qDebug()<<str;
        if(str=="OK")
        {
            //    QString ip ="192.168.50.163";
                ui->textBrowser_equip->setText(tcpsocketip.toString());
            //    int port = 7755;
            //    // 连接到服务器
               tcpsocket->connectToHost(tcpsocketip,tcpsocketPort);
                ui->textBrowser_equip->append("连接成功");
                QString jsonString =
                                    R"({
                                       "type": 1,
                                        })";
                        qDebug()<<jsonString;


                // 发送数据
                     QByteArray data =R"({
                                      "type": 1,
                                       })";
//                     tcpSocket.write(data.toStdString());
//                     tcpSocket.flush(); // 刷新缓冲区
//                     tcpSocket.waitForBytesWritten(); // 等待数据写入完成
        }
        qDebug()<<"返回信息";
        qDebug()<<datagram.data();
    }

}



void M0Wig::btnConnClickedSlot()
{/*//获得ip和端口号
    QString ip ="192.168.50.163";
    int port = 7755;

    // 连接到服务器
    tcpsocket->connectToHost(ip,port);*/



}




